package com.example.lay.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

/**
 * Created by Lay on 2014/12/9.
 */
public class MyBindService extends Service {

    private int count;
    private MyBinder binder = new MyBinder();
    private boolean quit;

    class MyBinder extends Binder {
        public int getCount() {
            return count;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    @Override
    public void onCreate() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (!quit) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    count++;
                }
            }
        }).start();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        System.out.println("onUnbinded");
        return true;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        System.out.println("onDestroyed");
        this.quit = true;
    }
}
